<?
//###########################################################################################
class Validate {
/*
init_validate_array = array(
	'%string%'=>array('add'=>%num%, 'edit'=>%num%, 'pattern'=>'%pattern%'),
	'%string%'=>array('add'=>%num%, 'edit'=>%num%, 'pattern'=>'%pattern%'),
);

flags(add, edit)
	1 dont pass
	2 check if passed
	3 check for sure
*/ 
var $init_validate_array = array();
var $validate_returned = array();
//*******************************************************************************************

function Validate($validate_array){
	if(!$this->valid_validator($validate_array)){
		return false;
	}
	$this->init_validate_array = $validate_array;
	return true;
}
//*******************************************************************************************

function validate_value($key, $value){
	if($this->init_validate_array[$key] and preg_match($this->init_validate_array[$key]['pattern'], $value)){
		return true;
	}else{	
		return false;
	}
}
//*******************************************************************************************

function validate_array($key_array, $mode, $strict = false){
	$this->flush_validate_returned();
	$ret = true;

	if(!in_array($mode, array('add', 'edit'))){
		return false;
	}

	foreach($key_array as $k => $v){
		$check = $this->init_validate_array[$k][$mode];
		$pattern = $this->init_validate_array[$k]['pattern'];
		//echo '<br>';
		//print_r($this->init_validate_array[$k]);
		//echo "$k => $check , $mode , $pattern<br>";

		if($strict and empty($this->init_validate_array[$k])){
			$this->validate_returned[$k] = 0;
			$ret = false;
		}
		
		if(($check == 2 and $v == '') or ($check == 2 and preg_match($pattern, $v))){
			$this->validate_returned[$k] = 1;
		}elseif($check == 3 and $v != '' and preg_match($pattern, $v)){
			$this->validate_returned[$k] = 1;
		}else{
			$this->validate_returned[$k] = 0;
			$ret = false;
		}		
	}
	return $ret;		
}
//*******************************************************************************************

function flush_validate_returned(){
	$this->validate_returned = array();
	return true;
}
//*******************************************************************************************

function valid_validator($validate_array){
	foreach($validate_array as $k=>$v){
		if(!$this->valid_mask($v) or !$v['pattern']){
			return false;
		}
	}
	return true;
}
//*******************************************************************************************

function valid_mask($mask){
	if(!in_array($mask['add'], array(1, 2, 3)) or !in_array($mask['edit'], array(1, 2, 3))){
		return false;
	}
	return true;
}
//*******************************************************************************************

}
?>